408B - Garland - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

from heapq import heappush,heappop
from collections import deque

t = 1
for tc in range(1,t+1):
                    s = input()
    p = input()

    cols = {}
    for c in s:
        if c not in cols:
            cols[c]=[0,0]
        cols[c][0]+=1

    ans = 0
    for c in p:
        if c not in cols:
            ans=-1
            break
        cols[c][1]+=1

    if ans==-1:
        print(ans)
        continue

    for c in cols:
        ans+=min(cols[c])
    print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define nl "\n"
#define MOD 1000000009
#define pb push_back
#define ppb pop_back
#define dbg(x) cerr<<#x<<"-"<<x<<nl;
#define siz 100005

// solve function
void solve(){
    string a,b;
    cin>>a>>b;
    vector<ll>x(26,0);
    vector<ll>y(26,0);
    for(int i=0;i<(int)a.length();i++) ++x[a[i]-'a'];
    for(int j=0;j<(int)b.length();j++) ++y[b[j]-'a'];
    ll res = 0;
    for(int i=0;i<26;i++){
        if(x[i]==0 && y[i]!=0){
            cout<<-1<<nl;
            return;
        }
        res += min(x[i],y[i]);
    }
    cout<<res<<nl;
}

/* main function */
int main(){
    clock_t start,end;
    start=clock();
    fast();
    ll t=1;
    ll i=1;
    /* cin>>t; */
    while(t--){
        /* cout<<"Case# "<<i<<": "; */
        solve();
        i++;
    }
    end=clock();
    double time=double(end-start)/double(CLOCKS_PER_SEC);
    cerr<<nl<<"Time: "<<time<<" sec"<<nl;
}


Comments

Submit
0 Comments
More Questions

1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro